/*
 * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND 
 * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
 * CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
 * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 * 
 * Copyright (C) SEMTECH S.A.
 */
/*! 
 * \file       sx1276-Hal.c
 * \brief      SX1276 Hardware Abstraction Layer
 *
 * \version    2.0.B2 
 * \date       Nov 21 2012
 * \author     Miguel Luis
 *
 * Last modified by Miguel Luis on Jun 19 2013
 */
//#include <stdint.h>
//#include <stdbool.h> 

#include "platform.h"
#include "rs485_2.h"

#if defined( USE_SX1276_RADIO )

//#include "ioe.h"
//#include "spi.h"

#include "sys.h"
#include "sx1276-Hal.h"
#include "delay.h"
/*!
 * SX1276 RESET I/O definitions
 */
#define RESET_IOPORT                                GPIOE
#define RESET_PIN                                   GPIO_Pin_14

/*!
 * SX1276 SPI NSS I/O definitions
 */
#define NSS_IOPORT                                  GPIOE
#define NSS_PIN                                     GPIO_Pin_10     

/*!
 * SX1276 DIO pins  I/O definitions
 */
#define DIO0_IOPORT                                 GPIOE
#define DIO0_PIN                                    GPIO_Pin_15

#define DIO1_IOPORT                                 GPIOD
#define DIO1_PIN                                    GPIO_Pin_4

#define DIO2_IOPORT                                 GPIOD
#define DIO2_PIN                                    GPIO_Pin_3

#define DIO3_IOPORT                                 GPIOE
#define DIO3_PIN                                    GPIO_Pin_7

#define DIO4_IOPORT                                 GPIOE
#define DIO4_PIN                                    GPIO_Pin_8

#define DIO5_IOPORT                                 GPIOE
#define DIO5_PIN                                    GPIO_Pin_9

#define RXTX_IOPORT                                 
#define RXTX_PIN                                    FEM_CTX_PIN


#define RXE_PORT       			GPIOD
#define RXE_PIN  				    GPIO_Pin_6
#define RXE_CLOCK  				  RCC_AHB1Periph_GPIOD
#define RXE_HIGH()         		GPIO_SetBits(RXE_PORT,RXE_PIN)
#define RXE_LOW()          		GPIO_ResetBits(RXE_PORT,RXE_PIN)
#define RXE_STATE()        		GPIO_ReadOutputDataBit(RXE_PORT,RXE_PIN)

#define TXE_PORT       			GPIOD
#define TXE_PIN  				GPIO_Pin_7
#define TXE_CLOCK  				RCC_AHB1Periph_GPIOD
#define TXE_HIGH()         		GPIO_SetBits(TXE_PORT,TXE_PIN)
#define TXE_LOW()          		GPIO_ResetBits(TXE_PORT,TXE_PIN)
#define TXE_STATE()        		GPIO_ReadOutputDataBit(TXE_PORT,TXE_PIN)

void Set_RF_Switch_RX(void)
{
	RXE_HIGH();
	TXE_LOW();
}

void Set_RF_Switch_TX(void)
{
	RXE_LOW();
	TXE_HIGH();
}


// PD3 Lora_DIO2
// PD4 Lora_DIO1
// PD5 Lora_POWER
// PD6 Lora_RXEN
// PD7 Lora_TXEN
// PE7 Lora_DIO3
// PE8 Lora_DIO4
// PE9 Lora_DIO5
// PE10 Lora_NSS
// PE11 Lora_CLK
// PE12 Lora_MOSI
// PE13 Lora_MISO
// PE14 Lora_RST
// PE15 Lora_DIO0

void SX1276InitIo( void )
{
     GPIO_InitTypeDef GPIO_InitStructure;
     RCC_APB2PeriphClockCmd( RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE );
     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     // Configure SPI-->NSS as output
     GPIO_InitStructure.GPIO_Pin = NSS_PIN|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_14;
    GPIO_Init( NSS_IOPORT, &GPIO_InitStructure );
	  GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_SET );
	  GPIO_WriteBit( RESET_IOPORT, RESET_PIN, Bit_SET );
	
	  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_5;
	  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
	  GPIO_Init( GPIOD, &GPIO_InitStructure );
	  
	  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4;
	  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
	  GPIO_Init( GPIOD, &GPIO_InitStructure );
	
	  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_13|GPIO_Pin_15;
	  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
	  GPIO_Init( GPIOE, &GPIO_InitStructure );
		
	  GPIO_ResetBits(GPIOD,GPIO_Pin_5);
	  GPIO_SetBits(GPIOD,GPIO_Pin_6);
		
	  GPIO_SetBits(GPIOE,GPIO_Pin_14);
	  GPIO_ResetBits(GPIOD,GPIO_Pin_7);
	  delay_ms(100);
	  //printf("SX1276InitIo OK\r\n");
}

void SX1276SetReset( uint8_t state )
{
    GPIO_InitTypeDef GPIO_InitStructure;

    if( state == RADIO_RESET_ON )
    {
        // Configure RESET as output
				GPIO_InitStructure.GPIO_Pin = RESET_PIN;
				GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
        GPIO_Init( RESET_IOPORT, &GPIO_InitStructure );
		
				// Set RESET pin to 0
        GPIO_WriteBit( RESET_IOPORT, RESET_PIN, Bit_RESET );
    }
    else
    {
				GPIO_InitStructure.GPIO_Pin =  RESET_PIN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
				GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_Init( RESET_IOPORT, &GPIO_InitStructure );
		
				// Set RESET pin to 1
        GPIO_WriteBit( RESET_IOPORT, RESET_PIN, Bit_SET );
    }
}

void SX1276Write( uint8_t addr, uint8_t data )
{
    SX1276WriteBuffer( addr, &data, 1 );
}

void SX1276Read( uint8_t addr, uint8_t *data )
{
    SX1276ReadBuffer( addr, data, 1 );
}


void SX1276Write_one_byte(uint8_t buffer)
{
  
	uint8_t i = 0;  
    uint8_t temp = 0;  
    for(i=0; i<8; i++) 
	{
        temp = ((buffer&0x80)==0x80)? 1:0;  //将data最高位保存到temp；
        buffer = buffer<<1;                   //data左移一位，将次高位变为最高位，用于下次取最高位；
        GPIO_ResetBits(GPIOE,GPIO_Pin_11); //CLK; //CPOL=0              //拉低时钟，即空闲时钟为低电平， CPOL=0；
                         //根据temp值，设置MOSI引脚的电平；
        if(temp==1)GPIO_SetBits(GPIOE,GPIO_Pin_12);
		else GPIO_ResetBits(GPIOE,GPIO_Pin_12);
        delay_us(10);                      //简单延时，可以定时器或延时函数实现
        GPIO_SetBits(GPIOE,GPIO_Pin_11); //CPHA=0              //拉高时钟， W25Q64只支持SPI模式0或1，即会在时钟上升沿采样MOSI数据；
        delay_us(10);  
     }
      GPIO_ResetBits(GPIOE,GPIO_Pin_11);

}

void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;

    //NSS = 0;
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_RESET );
	delay_us(10);
     // PE11 Lora_CLK
     // PE12 Lora_MOSI
     // PE13 Lora_MISO
	SX1276Write_one_byte(addr|0x80);
    //SpiInOut( addr | 0x80 );
    for( i = 0; i < size; i++ )
    {
        SX1276Write_one_byte( buffer[i] );
    }

    //NSS = 1;
    delay_us(1);
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_SET );
}


uint8_t SX1276Read_one_byte(void)
{
    uint8_t i = 0;
    uint8_t read_data = 0xFF;
    for(i=0; i<8; i++)
	{
        read_data = read_data << 1;  //“腾空” read_data最低位，8次循环后，read_data将高位在前；  
        GPIO_ResetBits(GPIOE,GPIO_Pin_11);                  //拉低时钟，即空闲时钟为低电平；  
        delay_us(10);
        GPIO_SetBits(GPIOE,GPIO_Pin_11);
        delay_us(10);
        if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_13)) 
		{ 
           read_data = read_data + 1;
        }
    }
     GPIO_ResetBits(GPIOE,GPIO_Pin_11);                      //最后SPI读取完后，拉低时钟，进入空闲状态  
    return read_data;

}
void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;
    //NSS = 0;
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_RESET );
	delay_us(10);
    SX1276Write_one_byte( addr & 0x7F );

    for( i = 0; i < size; i++ )
    {
        buffer[i] = SX1276Read_one_byte();
    }

    //NSS = 1;
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_SET );
	delay_us(10);
}

void SX1276WriteFifo( uint8_t *buffer, uint8_t size )
{
    SX1276WriteBuffer( 0, buffer, size );
}

void SX1276ReadFifo( uint8_t *buffer, uint8_t size )
{
    SX1276ReadBuffer( 0, buffer, size );
}

 uint8_t SX1276ReadDio0( void )
{
    return GPIO_ReadInputDataBit( DIO0_IOPORT, DIO0_PIN );
}

 uint8_t SX1276ReadDio1( void )
{
    return GPIO_ReadInputDataBit( DIO1_IOPORT, DIO1_PIN );
}

 uint8_t SX1276ReadDio2( void )
{
    return GPIO_ReadInputDataBit( DIO2_IOPORT, DIO2_PIN );
}

 uint8_t SX1276ReadDio3( void )
{
    return GPIO_ReadInputDataBit( DIO3_IOPORT, DIO3_PIN );
}

 uint8_t SX1276ReadDio4( void )
{
    return GPIO_ReadInputDataBit( DIO4_IOPORT, DIO4_PIN );
}

 uint8_t SX1276ReadDio5( void )
{
    return GPIO_ReadInputDataBit( DIO5_IOPORT, DIO5_PIN );
}


//射频芯片收发切换
 void SX1276WriteRxTx( uint8_t txEnable )
{
    if( txEnable != 0 )
    {
		Set_RF_Switch_TX(); //单片机将射频开关芯片切换成发射状态
//        IoePinOn( FEM_CTX_PIN );
//        IoePinOff( FEM_CPS_PIN );
    }
    else
    {
		Set_RF_Switch_RX();  //单片机将射频开关芯片切换成接收状态
//        IoePinOff( FEM_CTX_PIN );
//        IoePinOn( FEM_CPS_PIN );
    }
}

#endif // USE_SX1276_RADIO
